<template>
  <div class="h-full px-4 py-3 rounded-lg">
    <div class="flex justify-between">
      <div>
        <a-button class="ghost" @click="openModal('import')">
          添加任务
        </a-button>
      </div>
      <div class="flex justify-end">
        <a-input
          class="_input"
          v-model:value.trim="searchForm.name"
          placeholder="请输入任务名称"
          v-filter-special-chars
          maxLength="50"
        />
        <a-select
          class="_input"
          v-model:value="searchForm.status"
          allow-clear
          placeholder="选择任务状态"
          :options="statusList"
        />
        <SerchButtom @search="getList(true, true)" @reset="reset" />
      </div>
    </div>

    <div class="mt-3 h-[calc(100%-6rem)] overflow-y-auto">
      <a-table :dataSource="dataSource" :pagination="false" :columns="columns">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'order'">
            {{ (pagination.current - 1) * pagination.pageSize + index + 1 }}
          </template>
          <template v-else-if="column.dataIndex === 'operation'">
            <a-button
              type="link"
              :disabled="record.status === '1' || record.status === '3'"
              @click="viewUnrip(record)"
            >
              查看分割图
            </a-button>
            <a-button
              type="link"
              class="delete"
              :disabled="record.status === '1'"
              @click="openModal('delete', record)"
            >
              删除
            </a-button>
          </template>
        </template>
      </a-table>
    </div>

    <div class="mt-3 h-8">
      <Pagination @getListByPage="getListByPage" ref="paginationRef" />
    </div>
  </div>

  <IndexModal ref="indexModalRef" @isOk="getList(true, true)" />
</template>
<script lang="ts" setup>
import { ref, reactive, onMounted, onBeforeUnmount } from 'vue'
import { useRouter } from 'vue-router'

import IndexModal from './IndexModal.vue'
import SerchButtom from '@/components/YuanQi-SearchButtom.vue'

import { getDict } from '@/api/dict'
import { list } from './serve.api'

const router = useRouter()

const dataSource = ref()
const loading = ref<boolean>(false)
const paginationRef = ref()
const statusList = ref<any[]>([])

let searchFormClone = {
  pageNum: 1,
  pageSize: 10,
  name: '',
  status: undefined
}

// 搜索参数
const searchForm = reactive({
  name: '',
  status: undefined
})

const columns = ref([
  {
    title: '序号',
    dataIndex: 'order',
    width: '7.5rem'
  },
  {
    title: '任务名称',
    dataIndex: 'name',
    key: 'name',
    ellipsis: true
  },
  {
    title: '图片数量',
    dataIndex: 'picNum',
    key: 'picNum',
    ellipsis: true,
    width: '7.5rem'
  },
  {
    title: '数据路径',
    dataIndex: 'dataPath',
    key: 'dataPath',
    ellipsis: true
  },
  {
    title: '任务状态',
    dataIndex: 'statusName',
    key: 'statusName',
    ellipsis: true,
    width: '10rem'
  },
  {
    title: '操作',
    dataIndex: 'operation',
    key: 'operation',
    ellipsis: true,
    width: '12rem'
  }
])

// 重置
const reset = () => {
  searchForm.name = ''
  searchForm.status = undefined
  getList(true, true)
}

const pagination = reactive({
  total: 0,
  pageSize: 10, // 每页中显示10条数据
  position: ['bottomCenter'],
  current: 1,
  showSizeChanger: true,
  showQuickJumper: true,
  showTotal: (total: number) => `共 ${total} 条`
})

// 初始化数据
const getList = (flag = false, isSearch = false) => {
  if (flag) {
    pagination.current = 1
    paginationRef.value.changeCurrent(pagination.current)
  }
  loading.value = true
  // dataSource.value = []

  const params = {
    name: searchForm.name,
    status: searchForm.status,
    pageNum: pagination.current,
    pageSize: pagination.pageSize
  }
  if (isSearch) {
    searchFormClone = JSON.parse(JSON.stringify(params))
  } else {
    params.name = searchFormClone.name
    params.status = searchFormClone.status
  }

  list(params)
    .then((res) => {
      if (res) {
        console.log(res, '无人机巡视任务列表')

        dataSource.value = res.list
        pagination.total = res.total

        paginationRef.value.changeTotal(pagination.total)
        loading.value = false
      } else {
        loading.value = false
      }
    })
    .catch(() => {
      loading.value = false
    })
}

const getListByPage = (page: number, pageSize: number) => {
  if (pagination.current !== page) {
    pagination.current = page
    getList()
  }
  if (pagination.pageSize !== pageSize) {
    pagination.pageSize = pageSize
    getList(true)
  }
}

// 新增/编辑/删除操作
type ModalType = 'import' | 'device' | 'delete'
const indexModalRef = ref()

const openModal = (type: ModalType, data?: any) => {
  indexModalRef.value.showModal(type, data)
}

function viewUnrip(item: any) {
  router.push({
    path: '/patrolResult/patrolResultAnalyze',
    query: {
      id: item.id,
      name: encodeURIComponent(item.name)
    }
  })
}

onMounted(() => {
  getDict('uav_job_status').then((res) => {
    statusList.value = res.map((item: any) => ({
      label: item.dictLabel,
      value: item.dictValue
    }))
  })

  getList(true, true)
})

let timer: any = null

timer = setInterval(() => {
  getList()
}, 5000)

onBeforeUnmount(() => {
  clearInterval(timer)
})
</script>
<style lang="less" scoped>
._input {
  width: 13.125rem;
  vertical-align: middle;

  &:not(:first-child) {
    margin-left: 1.25rem;
  }
}
</style>
